from event_rule.dwc import NO_BET_AMOUNT_GAME_TYPE, NO_WIN_AMOUNT_GAME_TYPE

GAME_STAGE_ANALYSIS_CONFIG = {
    'real_time_report': False,
    "dynamic_argument": True,
    'interval_type_list': ['day'],
    'day_to_calc': [0, 1],
    'target_event_id': ['bet_order'],
    'values': [
        {
            "value_id": "bet_user",
            "target_value": "_user_id",
            "value_name": "参与人数",
            "filter": [
                {'term': {"_event_id": 'bet_order'}},
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "指定区间用户的参与总人数"
        },
        {
            "value_id": "_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "投注金额",
            "filter": [],
            "value_type": "float",
            "adjust_amount": True,
            "hide_result": True,
        },
        {
            "value_id": "lose_award_amount",
            "target_value": "bet_order_award_amount",
            "value_name": "负扣金额",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_BET_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"lt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "invalid_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "无效投注",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_WIN_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"gt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "valid_bet_amount",
            "value_name": "有效投注",
            "value_expr": "_bet_amount - invalid_bet_amount + (lose_award_amount * -1)",
            "adjust_amount": True,
            "hide_result": True,
            "description": "斗地主/血拼牛牛”：赢家”投注金额总和，\n诈金花/梭哈：”输家”投注金额总和，\n其余游戏等同投注金额"
        },
        {
            "value_id": "bet_amount",
            "value_name": "参与金额",
            "value_expr": "_bet_amount - lose_award_amount",
            "adjust_amount": True,
            "description": "指定区间用户的参与总金额",
        },
        {
            "value_id": "tax_amount",
            "target_value": 'bet_order_tax_amount',
            "value_name": "玩法税收",
            "filter": [],
            "value_type": "float",
            "adjust_amount": True,
            "description": "指定区间用户产生的总税收"
        },
        {
            "value_id": "choushui",
            "value_name": "玩法盈亏",
            "value_expr": "_choushui",
            "adjust_amount": True,
            "description": "指定区间用户的玩法盈亏汇总"
        },
        {
            "value_id": "_choushui",
            "target_value": ['bet_order_award_amount', '-', 'bet_order_bet_amount'],
            "value_name": "抽水金额",
            "filter": [],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "stay_2_bet_user",
            "target_value": "_user_id",
            "value_name": "次流人数",
            "filter": [
                {'term': {'_event_id': 'bet_order'}},
                {'terms': {'_user_id': '${bet_user_pool}'}}
            ],
            "time_range": {
                "_event_time": [1, 1]
            },
            "operation": "cardinality",
            "value_type": "long",
            "description": "参与指定玩法且参与金额在指定区间的用户第二天继续参与指定玩法的人数"
        },
        {
            "value_id": "bet_order_stay_rate_2",
            "value_name": "次留率",
            "value_expr": "stay_2_bet_user / bet_user",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "次留人数/参与人数"
        },
        {
            "value_id": "profit_user",
            "target_value": "_user_id",
            "value_name": "盈利人数",
            "filter": [
                {'term': {"_event_id": 'bet_order'}},
                {'terms': {'_user_id': '${profit_user_pool}'}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "指定区间的用户玩法盈利为正的人数"
        },
        {
            "value_id": "profit_user_profit_amount",
            "target_value": ['bet_order_award_amount', '-', 'bet_order_bet_amount'],
            "value_name": "盈利金额",
            "filter": [
                {"term": {"_event_id": "bet_order"}},
                {"terms": {"_user_id": "${profit_user_pool}"}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "指定区间的用户玩法盈利为正的人数盈利的总金额"
        },
        {
            "value_id": "stay_2_profit_user",
            "target_value": "_user_id",
            "value_name": "盈利次流人数",
            "filter": [
                {'term': {'_event_id': 'bet_order'}},
                {'terms': {'_user_id': '${profit_user_pool}'}}
            ],
            "time_range": {
                "_event_time": [1, 1]
            },
            "operation": "cardinality",
            "value_type": "long",
            "description": "指定区间的用户当天盈利且第二天继续参与指定玩法的人数"
        },
        {
            "value_id": "profit_stay_rate_2",
            "value_name": "盈利次留率",
            "value_expr": "stay_2_profit_user / profit_user",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "盈利次留人数/盈利人数"
        },
        {
            "value_id": "lose_user",
            "target_value": "_user_id",
            "value_name": "亏损人数",
            "filter": [
                {'term': {"_event_id": 'bet_order'}},
                {'terms': {'_user_id': '${lose_user_pool}'}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "指定区间的用户玩法盈利为负的人数"
        },
        {
            "value_id": "lose_user_profit_amount",
            "target_value": ['bet_order_award_amount', '-', 'bet_order_bet_amount'],
            "value_name": "亏损金额",
            "filter": [
                {"term": {"_event_id": "bet_order"}},
                {"terms": {"_user_id": "${lose_user_pool}"}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "指定区间的用户玩法盈利为负的人数亏损的总金额"
        },
        {
            "value_id": "lose_stay_2_user",
            "target_value": "_user_id",
            "value_name": "亏损次流人数",
            "filter": [
                {'term': {"_event_id": 'bet_order'}},
                {'terms': {'_user_id': '${lose_user_pool}'}}
            ],
            "time_range": {
                "_event_time": [1, 1]
            },
            "operation": "cardinality",
            "value_type": "long",
            "description": "指定区间的用户玩法盈利为负的人数亏损的总金额"
        },
        {
            "value_id": "lose_stay_rate_2",
            "value_name": "亏损次留率",
            "value_expr": "lose_stay_2_user / lose_user",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "亏损次留人数/亏损人数"
        }
    ]
}
